package com.kujie.im.config;

import com.easemob.im.ApiClient;
import com.easemob.im.ApiException;
import com.easemob.im.api.*;
import com.kujie.im.config.properties.HuanXinProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Like
 * @Date: 2025/10/10 10:56
 * @Description:
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(HuanXinProperties.class)
public class HuanXinConfig {

    @Autowired
    private HuanXinProperties huanXinProperties;

    /**
     * 环信API客户端
     *
     * @return ApiClient
     */
    @Bean
    public ApiClient service() {
        ApiClient apiClient = null;
        try {
            apiClient = ApiClient.builder()
                    .setBasePath(huanXinProperties.getBasePath())
                    .setAppKey(huanXinProperties.getAppKey())
                    .setClientId(huanXinProperties.getClientId())
                    .setClientSecret(huanXinProperties.getClientSecret())
                    .setMaxIdleConnections(huanXinProperties.getHttpMaxIdleConnections())
                    .setConnectKeepAliveMilliSeconds(huanXinProperties.getConnectKeepAliveMilliSeconds())
                    .setConnectTimeoutMilliSeconds(huanXinProperties.getConnectTimeoutMilliSeconds())
                    .build();
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        return apiClient;
    }
}
